package com.ruipengkj.view.memberView;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.Date;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

import com.ruipengkj.bean.TShopMsg;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.commons.util.Util;
import com.ruipengkj.component.JLabelJTextAreaComponent;
import com.ruipengkj.component.JLabelJTextFieldComponent;
import com.ruipengkj.component.RPJButtonBlue;
import com.ruipengkj.component.RPJButtonGray;
import com.ruipengkj.service.TShopMsgService;
import com.ruipengkj.view.base.BaseDialog;

public class MemberShopMsgUpdate extends BaseDialog {
	private static final long serialVersionUID = 1L;
	private JButton okButton;//确定
	private JButton cancel;//取消
	private JLabelJTextFieldComponent messageID = null;//新闻编号
	private JLabelJTextFieldComponent title = null;//标题
	private JLabelJTextAreaComponent content = null; //内容
	private JLabelJTextAreaComponent summary = null; //描述
	private TShopMsg tShopMsgDialog;
	public boolean isSuccess=false;  //判断是否成功
	
	private TShopMsgService tShopMsgService=BuildSpringApplication.getInstance().getTShopMsgServiceImpl();
	
	public MemberShopMsgUpdate(JDialog jd, String title, boolean b) {
		
		super(jd,title,b);
		this.init();
		this.setSize(300, 550);
		this.setLocationRelativeTo(null);
		this.setResizable(false);
	}
	private void init(){
		this.initViews();
		this.initListener();
		//this.getElements();
	}
	private void initListener(){
		okButton.addActionListener(this);
		cancel.addActionListener(this);
		okButton.registerKeyboardAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_C, KeyEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		Date date=new Date();
		TShopMsg tShopMsg = new TShopMsg();
		tShopMsg.setMessageID(messageID.getText().getText().trim());
		String titles=title.getText().getText().trim(); //充值金额
		String contents=content.getArea().getText().trim();
		String summarys=summary.getArea().getText().trim();
			if(e.getSource()==okButton){
				//确定
				if(titles.equals("")){
					JOptionPane.showMessageDialog(null, "标题不能为空！");
					title.getText().requestFocus();
					return;
				}else if(contents.equals("")){
					JOptionPane.showMessageDialog(null, "新闻内容不能为空！");
					content.getArea().requestFocus();
					return;
				}else if(summarys.equals("")){
					JOptionPane.showMessageDialog(null, "新闻描述不能为空！");
					summary.getArea().requestFocus();
					return;
				}
				tShopMsg.setType(tShopMsgDialog.getType());
				tShopMsg.setTitle(titles);
				tShopMsg.setContent(contents);
				tShopMsg.setSummary(summarys);
				tShopMsg.setCreateTime(date);
				tShopMsg.setViewCount(tShopMsgDialog.getViewCount());
				try {
					tShopMsgService.saveOrUpdateService(tShopMsg);
					isSuccess=true;
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					isSuccess=false;
					e1.printStackTrace();
				}

				this.dispose();
			}else if(e.getSource() ==cancel){
				this.dispose();
			}
		
	}
	private void initViews() {
		JPanel main_jp=new JPanel();
		BoxLayout boxLayout=new BoxLayout(main_jp,BoxLayout.Y_AXIS);
		main_jp.setLayout(boxLayout);
		main_jp.setBackground(Util.PANEL_COLOR);
		messageID=new JLabelJTextFieldComponent("新闻编号：",30);
		title = new JLabelJTextFieldComponent("新闻标题：", 30);
		content=new JLabelJTextAreaComponent("新闻内容：",30,5);
		summary=new JLabelJTextAreaComponent("新闻描述：",30,5);
		messageID.getText().setEditable(false);
		JPanel jp1=new JPanel();
		okButton=new RPJButtonBlue("确定");
		cancel = new RPJButtonGray("取消");
		jp1.add(okButton);
		jp1.add(cancel);
		jp1.setOpaque(false);
		
		main_jp.add(messageID);
		main_jp.add(title);
		main_jp.add(content);
		main_jp.add(summary);
		main_jp.add(jp1);
		
		this.add(main_jp);
	}
	public void showDialog(){
		this.setVisible(true);
	}
	public void showDialog(TShopMsg tShopMsg){
		if(tShopMsg!=null){
			tShopMsgDialog = tShopMsg;
			messageID.getText().setText(tShopMsg.getMessageID());
			title.getText().setText(tShopMsg.getTitle());
			content.getArea().setText(tShopMsg.getContent());
			summary.getArea().setText(tShopMsg.getSummary());
		}
		this.setVisible(true);
	}
}
